package com.microservice.consumer.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/ribbon")
public class RibbonController {


    // 这个 bean 在 启动类中定义，如果定义在这里会报错，说循环依赖
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/trigger/{producer}")
    public String useRibbonDoLoadBalance(@PathVariable String producer) {
        String serviceUri = String.format("http://%s/produce/data/test-data", producer);    //使用的服务名称
        System.out.println("=====Url===== " + serviceUri);
        ResponseEntity<String> data = restTemplate.exchange(serviceUri, HttpMethod.GET, null, String.class);
        return data.getBody();
    }
}
